<template>
    <el-card shadow="never">
        <el-form :model="rows" label-width="250">
            <el-form-item label="充电平台:">
                <el-select v-model="rows.channel" placeholder="请选择" style="width: 100%">
                    <el-option :label="item" :value="key" v-for="(item,key) in channel"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="停车场编号:">
                <el-input type="text" v-model="rows.code"></el-input>
            </el-form-item>
            <el-form-item label="绑定商户:">
                <el-select v-model="rows.merch_id" placeholder="请选择" style="width: 100%">
                    <el-option :label="item" :value="key" v-for="(item,key) in merch"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="优惠规则:">
                <el-select v-model="rows.trigger" placeholder="请选择" style="width: 100%">
                    <el-option :label="item" :value="key" v-for="(item,key) in trigger"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="优惠规则配置:" v-if="rows.trigger">
                <Fieldlist :label='["最低"+trigger[rows.trigger],"指定停车券"]' :keys='["number","coupon_id"]' @change="changeField" :value="rows.rules_value">
                    <template #number="{list,row}">
                        <el-input v-model="list[row].number" type="number">
                            <template #append v-if="rows.trigger=='charge-time'">分钟</template>
                            <template #append v-if="rows.trigger=='charge-fee'">元</template>
                            <template #append v-if="rows.trigger=='charge-kwh'">kW·h（度）</template>
                        </el-input>
                    </template>
                    <template #coupon_id="{list,row}">
                        <el-select v-model="list[row].coupon_id" placeholder="请选择" style="width: 100%">
                            <el-option :label="item" :value="key" v-for="(item,key) in coupon"></el-option>
                        </el-select>
                    </template>
                </Fieldlist>
            </el-form-item>
            <el-form-item label="自定义停车规则:">
                <el-radio-group v-model="rows.use_diy_rules">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="0">否</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="选择停车规则:" v-if="rows.use_diy_rules">
                <el-select v-model="rows.rules_id" placeholder="请选择" style="width: 100%">
                    <el-option :label="item" :value="key" v-for="(item,key) in rules"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">保存</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
import fieldlist from "@components/Fieldlist.js";
export default{
    components:{
        'Fieldlist':fieldlist
    },
    data:{
        channel:Yunqi.data.channel,
        trigger:Yunqi.data.trigger,
        coupon:Yunqi.data.coupon,
        merch:Yunqi.data.merch,
        rules:Yunqi.data.rules,
        rows:Yunqi.data.charge?Yunqi.data.charge:{
            channel: '',
            code:'',
            merch_id:'',
            trigger:'',
            rules_value:[],
            use_diy_rules:0,
            rules_id:''
        }
    },
    methods: {
        changeMode:function (e){
            this.rows.mode=e;
        },
        changeField:function (e){
            this.rows.rules_value=e;
        },
        onSubmit:function (){
            if(!this.rows.channel){
                Yunqi.message.error('请选择充电平台');
                return;
            }
            if(!this.rows.merch_id){
                Yunqi.message.error('请选择充电商户');
                return;
            }
            if(!this.rows.code){
                Yunqi.message.error('请输入停车场编号');
                return;
            }
            if(this.rows.rules_value.length===0){
                Yunqi.message.error('请设置优惠规则');
                return;
            }
            Yunqi.ajax.post('charge/setting',this.rows);
        }
    }
}
</script>
<style>
</style>